package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppUserDressup;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 购买坐骑
 */
@Repository
public class AppUserDressupDao extends BaseDao<AppUserDressup> {

    /**
     * 查询列表
     */
    public List<AppUserDressup> list(Map<String,Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_user_dressup where 1=1  ");
        if(StringEx.isNotNull(paraMap.get("startDate"))){
            sbStr.append(" and DATE_FORMAT(buyingtime, '%Y-%m-%d')  >=  ? ");
            paramList.add(paraMap.get("startDate"));
        }
        if(StringEx.isNotNull(paraMap.get("endDate"))){
            sbStr.append(" and DATE_FORMAT(buyingtime, '%Y-%m-%d')  <=  ? ");
            paramList.add(paraMap.get("endDate"));
        }
        if(StringEx.isNotNull(paraMap.get("nick"))){
            sbStr.append(" and nick like ? ");
            paramList.add("%"+paraMap.get("nick")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("phone"))){
            sbStr.append(" and phone = ? ");
            paramList.add(paraMap.get("phone"));
        }
        if(StringEx.isNotNull(paraMap.get("type"))){
            sbStr.append(" and type = ? ");
            paramList.add(paraMap.get("type"));
        }
        if(StringEx.isNotNull(paraMap.get("state"))){
            sbStr.append(" and state = ? ");
            paramList.add(paraMap.get("state"));
        }
        sbStr.append(" order by buyingtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppUserDressup.class,splitPage);
    }


    public List<AppUserDressup> list(Map<String,Object> paraMap) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_user_dressup where 1=1  ");
        if(paraMap.containsKey("today") && StringEx.isNotNull(paraMap.get("today"))) {
            sbStr.append(" and to_days(buyingtime) = to_days(now())");
        }
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppUserDressup.class);
    }


    /**
     * 详情
     */
    public AppUserDressup get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_user_dressup where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppUserDressup.class);
    }

    /**
     * 获取指定用户获取信息
     *
     * @param arr 动态id 拼接
     * @return
     * @return
     */
    public List<AppUserDressup> getPiliang(String arr,Integer type) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_user_dressup where type=?  ");
        paramList.add(type);
        sbStr.append(" and userid in ( ");
        sbStr.append(arr);
        sbStr.append(" ) ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppUserDressup.class);
    }

    /**
     * 详情
     */
    public AppUserDressup getUserid(Integer userid,Integer type) {
        if (StringEx.isNullOrLing(userid)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_user_dressup where 1=1 and state=1 ");
        sbStr.append(" and userid = ? ");
        paramList.add(userid);
        sbStr.append(" and type = ? ");
        paramList.add(type);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppUserDressup.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppUserDressup haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 更新我的装扮信息
     * @param goodname 头饰/坐骑名
     * @param goodpic 头饰/坐骑图片
     * @param type 1坐骑  2头饰
     * @param mountsid 头饰/坐骑id
     */
    public void updateZuoqiDate(Integer mountsid,Integer type,String goodpic,String goodname) {
        if(StringEx.isNull(mountsid)){
            return ;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuffer sql =new StringBuffer(" update app_user_dressup set goodpic=? ") ;
        paramList.add(goodpic);
        sql.append(" ,goodname=? ");
        paramList.add(goodname);
        sql.append(" where mountsid=? ");
        paramList.add(mountsid);
        sql.append(" and type=? ");
        paramList.add(type);
        this.update(sql.toString(),paramList);
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id <= 0) {
            return;
        }
        String sql = "delete  from app_user_dressup where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    /**
     * 后天管理获取自己未过期的坐骑
     * @param map
     * @param bean
     * @return
     */
    public List<AppUserDressup> findList(Map<String, Object> map, SplitPageBean bean) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" SELECT a.* FROM app_user_dressup a where 1=1 ");
        if (StringEx.isNotNull(map.get("userid"))) {
            sbStr.append(" and a.userid = ? ");
            paramList.add(map.get("userid"));
        }
        if (StringEx.isNotNull(map.get("type"))) {
            sbStr.append(" and a.type = ? ");
            paramList.add(map.get("type"));
        }
        sbStr.append(" and a.overtime > Now() order by a.buyingtime desc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppUserDressup.class, bean);
    }



    /**
     * 更新用户头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateImg(Integer userid,String pic){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(pic)){
            return;
        }
        String sql = " UPDATE app_user_dressup SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }
    /**
     * 更新用户昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateNick(Integer userid,String nick){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(nick)){
            return;
        }
        String sql = " UPDATE app_user_dressup SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    public List<AppUserDressup> selectAll() {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * FROM app_user_dressup as um GROUP BY um.id HAVING um.type != 3 && ((um.`day`*24*60*60)-SUM(timestampdiff(SECOND,um.buyingtime,now()))) <0");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppUserDressup.class);
    }

    public AppUserDressup selectByUserid(Integer userid, Integer mountsid, Integer type) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * from app_user_dressup where userid = ? and mountsid = ? and type = ?");
        paramList.add(userid);
        paramList.add(mountsid);
        paramList.add(type);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppUserDressup.class);
    }

    /**
     * 根据用户id查询未过期的坐骑、头饰信息
     * @param userid
     * @param type
     * @return
     */
    public List<AppUserDressup> selectMountsByUserid(Integer userid,Integer type) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("SELECT * from app_user_dressup where userid = ? and type = ? and overtime > now()");
        paramList.add(userid);
        paramList.add(type);
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppUserDressup.class);
    }
    /**
     * 查询未过期的坐骑、头饰信息
     * @return
     */
    public List<AppUserDressup> selectNoGuoqi() {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("SELECT * from app_user_dressup where state=1 ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppUserDressup.class);
    }

}
